#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/**
 * ${copyright}
 * Copyright (c) 2019. All Rights Reserved.
 */
package ${package}.oauth.handler;

import ${package}.common.bean.vo.JsonResult;
import ${package}.common.emum.ReturnCodeEnum;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: oauth2权限不足的情况异常类
 *
 * @author ywh
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Component
public class YkAccessDeniedHandler implements AccessDeniedHandler {

    private static final Logger LOG = LoggerFactory.getLogger(YkAccessDeniedHandler.class);

    /**
     * Description:
     * <无法补充>
     * @author ywh
     * @date 14:30 2019/8/8
     * @param httpServletRequest 1
     * @param httpServletResponse 2
     * @param accessDenied 3
     **/
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException accessDenied) throws IOException, ServletException {
        // vue中axios的response拦截器只拦截200状态的数据，否则走的是error了
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
        try {
            LOG.error(ReturnCodeEnum.PERMISSION_DENIED.getMsg());
            httpServletResponse.getWriter().write(JSON.toJSONString(JsonResult.getInstant(ReturnCodeEnum.PERMISSION_DENIED)));
            httpServletResponse.getWriter().flush();
            httpServletResponse.getWriter().close();
        } catch (IOException ex) {
            LOG.error("response error", ex);
        }
    }
}